package ngat.oss.util;

import ngat.oss.*;

import ngat.phase2.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.util.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/** Fix all execution times in proposal.*/
public class JYDFixExecTime {

    public static void main (String[] args) {

	String pname  = args[0];

	Proposal proposal = null;

	try {
	    File file = new File(pname);
	    ObjectInputStream fin = new ObjectInputStream(new FileInputStream(file));	
	    	    
	    try {
		proposal = (Proposal)fin.readObject();
		System.err.println("Fix::Read prop back from: "+file.getPath());
		fin.close();		   
		fin = null;
		file= null;
	    } catch (ClassNotFoundException cx) {
		proposal = null;
	    }

	} catch (Exception e) {
	    System.err.println("Fix:Error reading proposal: "+pname+" : "+e);
	    return;
	}

	if (proposal == null) {
	    System.err.println("Proposal was null");
	    return;
	}

	try {
	    TelescopeStatus.configureInstruments(new File("/occ/oss/config/instruments.properties"));
	} catch (Exception ix) {
            System.err.println("Fix:Error configuring instruments : "+ix);
            return;
        }


	Iterator ig = proposal.listAllGroups();
	while (ig.hasNext()) {
	    Group group = (Group)ig.next();
	    System.err.println("Preset exec for G: "+group.getName()+" as "+group.getNominalExecutionTime());
	    Scheduling.calculateExecutionTimes(group);
	    System.err.println("Recalc exec for G: "+group.getName()+" as "+group.getNominalExecutionTime());
	} 
	
	try {
	  File file = new File(pname);
	  ObjectOutputStream fout = new ObjectOutputStream(new FileOutputStream(file));
	  fout.flush();
	  fout.writeObject(proposal);
	  fout.flush();
	  fout.close();
	} catch (Exception e) {
	  System.err.println("WipeAg:Error storing proposal: "+pname+" : "+e);
	}
	
    }
    
}
